package worker.model.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import worker.model.dao.WorkOrderDao;
import worker.model.vo.HistoryVO;
import worker.model.vo.ItemInfoVO;
import worker.model.vo.ListVO;
import worker.model.vo.OperationsVO;
import worker.model.vo.PagingBean;
import worker.model.vo.WorkOrdersVO;

public class WorkOrderService {
	private WorkOrderDao workOrderDao;

	public void setWorkOrderDao(WorkOrderDao workOrderDao) {
		this.workOrderDao = workOrderDao;
	}

	public HashMap getWorkOrderIdandLastSerialNo() {
		System.out.println("WorkOrderService/getWorkOrderId()");
		String wo_id="";
		String last_wo_id="";
		WorkOrdersVO woVO=null;
		HashMap map= new HashMap();
		String sno_max="";
		int index=1;
		try {
			// 최초의 work order 
			woVO=(WorkOrdersVO)this.workOrderDao.getLastWorkOrderInfo();
			
			if(woVO==null){
				// 오늘 날짜  :20120525
				wo_id =index+"_"+(String)this.workOrderDao.getDatebyDaulTable();	
			}else{
				System.out.println("sno:::"+woVO.getSerial_no_max());
				sno_max= woVO.getSerial_no_max().substring(6,10);
				System.out.println("sno_max"+sno_max);
				// 마지막 work_order_id를 갖고온다.
				last_wo_id = woVO.getWork_order_id();
				String id[]= last_wo_id.split("_");
				if(id[1].equals(this.workOrderDao.getDatebyDaulTable())){
					index= Integer.parseInt(id[0])+1;
					wo_id=index+"_"+id[1];
				}else{
					// 같은 날짜가 아닌경우, 새로운 index를 부여
					wo_id =index+"_"+(String)this.workOrderDao.getDatebyDaulTable();	
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		map.put("wo_id", wo_id);
		map.put("sno_max",sno_max);
		map.put("last_wo_id",last_wo_id);
		return map;
	}
	// Make a new Work Order SerialNo. 
	public HashMap getStartandEndSerialNo(int quantity) {
		System.out.println("WorkOrderService/getStartandEndSerialNo()::"+quantity);
		WorkOrdersVO woVO;
		String str_sno="";
		String end_sno="";
		HashMap map= new HashMap();
		try {
			woVO=(WorkOrdersVO)this.workOrderDao.getLastWorkOrderInfo();
			if(woVO==null){
				str_sno=this.workOrderDao.getDatebyDaulTable()+"0000";
				int sno=Integer.parseInt(str_sno);
				end_sno= Integer.toString(sno+quantity-1);
			}else{
				String index1= woVO.getSerial_no_max().substring(0,6);
				String index2= woVO.getSerial_no_max().substring(7,10);
			
				System.out.println("index1:"+index1);
				System.out.println("index2:"+index2);
				if(index1.equals(this.workOrderDao.getDatebyDaulTable())){
					str_sno=Integer.toString(Integer.parseInt(woVO.getSerial_no_max())+1);
					end_sno=Integer.toString(Integer.parseInt(woVO.getSerial_no_max())+quantity);
				}else{
					str_sno=this.workOrderDao.getDatebyDaulTable()+"0000";
					int sno=Integer.parseInt(str_sno);
					end_sno= Integer.toString(sno+quantity-1);
				}
			}
			map.put("str_sno", str_sno);
			map.put("end_sno", end_sno);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(map.toString());
		return map;
	}
	public void setWorkOrderInfo(WorkOrdersVO woVO) {
		System.out.println(woVO.toString());
		try {
			this.workOrderDao.setWorkOrderInfo(woVO);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/*
	 * histyor에 work order 입력
	 */
	public void setHistorybyWorkOrderInfo(HistoryVO hisVO) {
		try {
			System.out.println(hisVO.toString());
			this.workOrderDao.setHistorybyWorkOrderInfo(hisVO);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public OperationsVO getOperationsVO() {
		OperationsVO opVO= new OperationsVO();
		try {
			opVO=this.workOrderDao.getOperationsVO();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return opVO;
	}
	// work order 리스트 
	public ListVO getAllWorkOrderList(String page) {
		ArrayList<Map> wo_list = new ArrayList<Map>();
		ListVO lvo=null;
		HashMap map= new HashMap();
		try {
			wo_list = this.workOrderDao.getAllWorkOrderList(page);
			int total= this.workOrderDao.getWorkOrderSize();
			System.out.println(total);
			PagingBean paging = new PagingBean(total, Integer.parseInt(page));
			lvo = new ListVO(wo_list, paging);
			System.out.println("서비스..."+lvo.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lvo;
	}

	public ArrayList<ItemInfoVO> getItemList() {
		ArrayList<ItemInfoVO>  item_list=null;
		try {
			item_list = this.workOrderDao.getItemList();
			System.out.println(item_list.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item_list;
	}
}
